﻿using System;
using System.ComponentModel;
using Microsoft.Practices.CompositeUI.Collections;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.Utility;
using Microsoft.Practices.ObjectBuilder;

namespace Microsoft.Practices.CompositeUI
{
    /// <summary>
    /// 	Defines the work item into which smart parts run.
    /// </summary>
    public class WorkItem : IBuilderAware, IDisposable
    {
        #region Fields & Properties

        private Builder builder;
        private ILifetimeContainer lifetime = new LifetimeContainer();
        private IReadWriteLocator locator;

        private WorkItemStatus status;

        /// <summary>
        /// 	Gets the current <see cref = "WorkItemStatus" /> of the <see cref = "WorkItem" />.
        /// </summary>
        public WorkItemStatus Status
        {
            get
            {
                return status;
            }
        }

        private WorkItem _parent;

        /// <summary>
        /// 	Gets the parent <see cref = "WorkItem" />.
        /// </summary>
        [Browsable(false)]
        [Dependency(NotPresentBehavior = NotPresentBehavior.ReturnNull)]
        public WorkItem Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                if (buildUpFinished)
                {
                    throw new InvalidOperationException();
                }
                _parent = value;
            }
        }

        /// <summary>
        /// 	Gets the root <see cref = "WorkItem" /> (the one at the top of the hierarchy).
        /// </summary>
        [Browsable(false)]
        public WorkItem Root
        {
            get
            {
                var workItem = this;
                while (workItem.Parent != null)
                {
                    workItem = workItem.Parent;
                }
                return workItem;
            }
        }

        #endregion

        #region Collection Fields & Properties

        private ManagedObjectCollection<WorkItem> workItemCollection;

        /// <summary>
        /// 	Returns a collection describing the child <see cref = "WorkItem" /> objects in this WorkItem.
        /// </summary>
        public ManagedObjectCollection<WorkItem> WorkItems
        {
            get
            {
                return workItemCollection;
            }
        }

        private ManagedObjectCollection<object> smartPartCollection;

        /// <summary>
        /// 	Returns a collection describing the child smart parts (objects
        /// 	with the <see cref = "SmartPartAttribute" /> applied to them) in this WorkItem.
        /// </summary>
        public ManagedObjectCollection<object> SmartParts
        {
            get
            {
                return smartPartCollection;
            }
        }

        private ManagedObjectCollection<Command> commandCollection;

        /// <summary>
        /// 	List of commands registered with the <see cref = "WorkItem" />.
        /// </summary>
        public ManagedObjectCollection<Command> Commands
        {
            get
            {
                return commandCollection;
            }
        }

        private ManagedObjectCollection<object> itemsCollection;

        /// <summary>
        /// 	Gets a list of all the objects and services contained in this <see cref = "WorkItem" />.
        /// </summary>
        public ManagedObjectCollection<object> Items
        {
            get
            {
                return itemsCollection;
            }
        }

        private ManagedObjectCollection<IWorkspace> workspaceCollection;

        /// <summary>
        /// 	Returns a collection describing the <see cref = "IWorkspace" /> objects in this WorkItem.
        /// </summary>
        public ManagedObjectCollection<IWorkspace> Workspaces
        {
            get
            {
                return workspaceCollection;
            }
        }

        private ManagedObjectCollection<EventTopic> eventTopicCollection;

        /// <summary>
        /// 	Returns a collection describing the <see cref = "EventTopic" /> objects in this WorkItem.
        /// </summary>
        public ManagedObjectCollection<EventTopic> EventTopics
        {
            get
            {
                return eventTopicCollection;
            }
        }

        private ServiceCollection serviceCollection;

        /// <summary>
        /// 	Returns the collection of services associated with this WorkItem.
        /// </summary>
        public ServiceCollection Services
        {
            get
            {
                return serviceCollection;
            }
        }

        private UIExtensionSiteCollection uiExtensionSiteCollection;

        /// <summary>
        /// 	Returns a collection of <see cref = "UIExtensionSite" />s in the WorkItem.
        /// </summary>
        public UIExtensionSiteCollection UIExtensionSites
        {
            get
            {
                return uiExtensionSiteCollection;
            }
        }

        #endregion

        #region State Fields & Properties

        private bool buildUpFinished;

        #endregion

        #region Events

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is activated.
        /// </summary>
        public event EventHandler Activated;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is activating.
        /// </summary>
        public event CancelEventHandler Activating;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is deactivated.
        /// </summary>
        public event EventHandler Deactivated;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is deactivated.
        /// </summary>
        public event CancelEventHandler Deactivating;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is disposed.
        /// </summary>
        public event EventHandler Disposed;

        /// <summary>
        /// 	Occurs when the <see cref = "ID" /> is changed.
        /// </summary>
        public event EventHandler<DataEventArgs<string>> IdChanged;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is initialized.
        /// </summary>
        public event EventHandler Initialized;

        /// <summary>
        /// 	Occurs when the <see cref = "Run" /> method is executed.
        /// </summary>
        public event EventHandler RunStarted;

        /// <summary>
        /// 	Occurs when the <see cref = "WorkItem" /> is terminated.
        /// </summary>
        public event EventHandler Terminated;

        /// <summary>
        /// 	Occurs before a <see cref = "WorkItem" /> is terminated.
        /// </summary>
        public event EventHandler Terminating;

        internal event EventHandler<DataEventArgs<object>> ObjectAdded;
        internal event EventHandler<DataEventArgs<object>> ObjectRemoved;

        #endregion

        #region Implementation of IBuilderAware

        public void OnBuiltUp(string id)
        {
            throw new NotImplementedException();
        }

        public void OnTearingDown()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// 	Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            //Dispose(true);
            //GC.SuppressFinalize(this);
        }

        #endregion
    }
}